﻿using System;
using ilgolosone.DomainModel.Entities;
using ilgolosone.DomainModel.Repositories;
using ilgolosone.Shared;
using ilgolosone.Shared.Data;
using NHibernate.Tool.hbm2ddl;
using ilgolosone.Persistence.NH.Data;

namespace ilgolosone.DataLoader
{
    class Program
    {
        static void Main(string[] args)
        {
            Bootstrapper.Initialize();            

            INHibernateUnitOfWork unitOfWork = Shared.IoC.DependencyResolver.Resolve<INHibernateUnitOfWork>();            

            ISessionCreator creator = Shared.IoC.DependencyResolver.Resolve<ISessionCreator>();

            creator.GenerateSchema();
            
            using (unitOfWork.Start())
            {
                var specialtyRepository = Shared.IoC.DependencyResolver.Resolve<ISpecialtyRepository>();
                var restaurantRepository = Shared.IoC.DependencyResolver.Resolve<IRestaurantRepository>();                

                specialtyRepository.DeleteAll();
                restaurantRepository.DeleteAll();                
                
                unitOfWork.Flush();

                for (int i = 0; i < 20; i++)
                {
                    Specialty specialty = new Specialty("Specialty "+i,"Specialty " + i + " Description","pix1.jpg");
                    specialtyRepository.Save(specialty);

                    Restaurant rest = new Restaurant("Restaurant " + i);
                    
                    Dish dish = new Dish("Dish " + i, DishCategories.Pasta, specialty, rest, "ingredient " + i, 10);

                    unitOfWork.CurrentSession.Save(dish);

                    rest.Dishes.Add(dish);

                    restaurantRepository.Save(rest);

                }

                unitOfWork.Commit();
            }

            Console.WriteLine("Done...");
        }
    }
}
